package com.example.moshudriver.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.BeeFramework.model.BusinessResponse;
import com.BeeFramework.view.ToastView;
import com.sgr.dykdriver.R;
import com.example.moshudriver.model.DrawModel;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.withdrawmoneyResponse;
import com.external.androidquery.callback.AjaxStatus;

public class WithdrawActivity extends Activity implements OnClickListener,BusinessResponse{
 TextView	top_view_title,top_view_right_text;
 Button withward_comfirm;
 EditText recharge_edit,alipay_account,tv_real_name,pay_pwd;
 ImageView top_view_back;
 UserBalanceModel withdrawModel;
 DrawModel withdraw_list;
 LinearLayout lin_pay;
private LinearLayout ll_alipay;
private LinearLayout ll_realname;
private LinearLayout ll_money;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_withward);
		withdrawModel=new UserBalanceModel(this);
		withdrawModel.addResponseListener(this);
		withdraw_list=new DrawModel(this);
		withdraw_list.addResponseListener(this);
		withdrawModel.isPassword();//判断是否设置了密码
		InitView();
	}
    public void InitView(){
    	ll_alipay = (LinearLayout) findViewById(R.id.ll_alipay);
    	ll_realname = (LinearLayout) findViewById(R.id.ll_realname);
    	ll_money = (LinearLayout) findViewById(R.id.ll_money);
    	alipay_account=(EditText)findViewById(R.id.alipay_account);
    	tv_real_name=(EditText)findViewById(R.id.tv_real_name);
    	pay_pwd=(EditText)findViewById(R.id.pay_pwd);
    	lin_pay=(LinearLayout)findViewById(R.id.lin_pay);
    	top_view_title=(TextView)findViewById(R.id.top_view_title);
    	top_view_title.setText(R.string.withdraw);
    	recharge_edit=(EditText)findViewById(R.id.recharge_edit);
    	withward_comfirm=(Button)findViewById(R.id.withward_comfirm);
    	top_view_back=(ImageView)findViewById(R.id.top_view_back);
    	top_view_right_text=(TextView)findViewById(R.id.top_view_right_text);
    
    	top_view_right_text.setText(R.string.withdraw_record);
    	top_view_right_text.setOnClickListener(this);
    	top_view_back.setOnClickListener(this);
    	withward_comfirm.setOnClickListener(this);
	}
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.withward_comfirm:	
			if(withward_comfirm.getText().equals("提现")){
				if(recharge_edit.length()<=0){
					Toast.makeText(this, R.string.fill_in_money, 2000).show();
					
				}else if(alipay_account.length()<=0){
					Toast.makeText(this, "请输入支付宝账户", 2000).show();
				}else if(tv_real_name.length()<=0){
					Toast.makeText(this, "请输入真实姓名", 2000).show();
				}else if(pay_pwd.length()<=0){
					Toast.makeText(this, "请输入支付密码", 2000).show();
				}
				else{
					withdrawModel.withdrawMoney(recharge_edit.getText().toString(),alipay_account.getText().toString(),
							tv_real_name.getText().toString(),pay_pwd.getText().toString());
				}
				
			}else{
				Intent intent = new Intent(WithdrawActivity.this, Withdraw_PasswordActivity.class);
				startActivity(intent);
			}
			
			
			 
			break;

		case R.id.top_view_back:
			finish();
			break;
			
			
			
		case R.id.top_view_right_text:
			Intent intent1=new Intent();
			intent1.setClass(WithdrawActivity.this,Withdraw_recordActivity.class);
			startActivity(intent1);
			break;
		}
		
	}
	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		if(url.endsWith(ApiInterface.WITHDRAW_MONEY)){
			pay_pwd.setText("");
			recharge_edit.setText("");
			Toast.makeText(this, R.string.withdraw_success, 2000).show();
		}
		if(url.endsWith(ApiInterface.WITHDRAW_IS_PWD)){
			withdrawmoneyResponse response = new withdrawmoneyResponse();
            response.fromJson(jo);
			
			if(response.ispwd==1){
				top_view_right_text.setVisibility(View.VISIBLE);
				ll_alipay.setVisibility(View.VISIBLE);
				ll_realname.setVisibility(View.VISIBLE);
				ll_money.setVisibility(View.VISIBLE);
				lin_pay.setVisibility(View.VISIBLE);
				withward_comfirm.setVisibility(View.VISIBLE);
				top_view_right_text.setVisibility(View.VISIBLE);
				withward_comfirm.setText("提现");
				
			}else{//没有设置密码
				ToastView toastView=new ToastView(WithdrawActivity.this, "您未设置提现密码，请先设置提现密码！");
				toastView.setGravity(Gravity.CENTER, 0, 0);
				toastView.setDuration(3000);
				toastView.show();
				top_view_right_text.setVisibility(View.GONE);
				ll_alipay.setVisibility(View.GONE);
				ll_realname.setVisibility(View.GONE);
				ll_money.setVisibility(View.GONE);
				lin_pay.setVisibility(View.GONE);
				withward_comfirm.setVisibility(View.VISIBLE);
				withward_comfirm.setText("设置支付密码");
			}
		
		}
		
	}
	


}
